import { IEventListPublishPathListReq } from './interface'

export const normalizeReqData = <T extends IEventListPublishPathListReq | null>(reqData: T): T => {
  if (reqData) {
    if (reqData.searchWords) {
      const newSearchWords = (Array.isArray(reqData.searchWords) ? reqData.searchWords : [reqData.searchWords]).map(
        (word: string) => word.trim().replace(/\s*\n+\s*/g, '\n'),
      )
      if (
        (Array.isArray(reqData.searchWords) ? reqData.searchWords : [reqData.searchWords]).join(',') !==
        newSearchWords.join(',')
      ) {
        reqData = { ...reqData, searchWords: newSearchWords }
      }
    }
  }
  return reqData
}

export const getSearchFieldName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return 'FBA ID' as const
      case 2:
        return '运单ID' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2] },
)

export const getVisibleFlagName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '是' as const
      case 0:
        return '否' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 0] },
)

export const getShipTrackRequireName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '必须' as const
      case 2:
        return '可选' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2] },
)

export const getShipTrackStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '不同步' as const
      case 2:
        return '同步成功' as const
      case 3:
        return '同步失败' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3] },
  {
    colorMap: {
      1: 'default',
      2: 'success',
      3: 'error',
    },
  },
)

export const getLocationTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '始发地' as const
      case 2:
        return '集货仓' as const
      case 3:
        return '起运港' as const
      case 4:
        return '目的港' as const
      case 5:
        return '中转仓' as const
      case 6:
        return '目的地' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4, 5, 6] },
)
